'''
发送get请求
1. 不带参数的请求
2. 带参数的请求，参数可以直接拼接到URL的后面  ? 后面为参数，key=value&key=value
3. 带参数的请求，参数使用params传递。
'''

import requests

# 百度搜索的接口，搜索的内容为：fiddler
# url = "http://www.baidu.com/s?wd=fiddler"
# 百度首页
url = "http://www.baidu.com"
# 发送get请求，响应的结果存储到变量r中
r = requests.get(url)
# 打印响应体
print(r.text)

# 金融项目注册接口
host = "http://192.168.124.16:8088/"
# 前后不要加空格，加了空格执行报错
# 用例1：密码小于6位
url = host + "/futureloan/mvc/api/member/register?mobilephone=18012345678&pwd=12345"
r = requests.get(url)
# 打印响应体，实际结果
print(r.text)
r = r.json()  # 把响应体转成字典格式的
assert r['status'] == 0  # 根据key取到value，与预期结果（接口文档）做对比
assert r['code'] == '20108'
assert r['msg'] == '密码长度必须为6~18'

# 用例2：密码超过18位
url = host + "/futureloan/mvc/api/member/register?mobilephone=18012345678&pwd=1234567890123456789"
r = requests.get(url)
# 打印响应体，实际结果
print(r.text)
r = r.json()  # 把响应体转成字典格式的
# {"status":0,"code":"20108","data":null,"msg":"密码长度必须为6~18"}
assert r['status'] == 0  # 根据key取到value，与预期结果（接口文档）做对比
assert r['code'] == '20108'
assert r['msg'] == '密码长度必须为6~18'

# 用例3：手机号码为空
print("===============用例3：手机号码为空====================")
url = host + "/futureloan/mvc/api/member/register?mobilephone=&pwd=123456789"
r = requests.get(url)
print(r.json())
assert r.json()['status'] == 0
assert r.json()['code'] == '20103'
assert r.json()['msg'] == '手机号不能为空'

# 用例4：密码为空
print("===============用例4：密码为空====================")
url = host + "/futureloan/mvc/api/member/register"
cs = {"mobilephone": "18012345678", "pwd": "", "regname": "hello"}
r = requests.get(url, params=cs)  # 参数组装成字典，通过params传递。
print(r.json())
assert r.json()['status'] == 0
assert r.json()['code'] == '20103'
assert r.json()['msg'] == '密码不能为空'

print("==========1===============")
print(type(r))  # 查看类型   <class 'requests.models.Response'>
print("==========2===============")
print(r.text)  # 文本格式的响应体
print(type(r.text))  # str
print("===========3==============")
print(r.json())  # 如果是json格式的，可以调用json()将其转成字典
print(type(r.json()))  # dict
print("===========4==============")
print(r.headers)  # 响应头
print("===========5==============")
print(r.request.headers)  # 请求头    user-agent
print("===========6==============")
print(r.status_code)  # 状态码
assert r.status_code == 200
